package cn.yunhe.service.serviceImpl;

import cn.yunhe.dao.OrderDao;
import cn.yunhe.dao.ProductDao;
import cn.yunhe.entity.Product;
import cn.yunhe.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private OrderDao orderDao;
    @Override
    public List<Product> findAll() {

        return productDao.findAll();
    }

    @Override
    public void insertProduct(Product product) {

        productDao.addProduct(product);
    }

    @Override
    public void deleteById(Integer id) {
        orderDao.updateProductIdByProductId(id);
        productDao.deleteById(id);
    }

    @Override
    public Product selectById(Integer id) {
        return productDao.selectById(id);
    }

    @Override
    public void updateById(Product product) {
        productDao.updateById(product);
    }

    @Override
    public void selectDelete(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            orderDao.updateProductIdByProductId(ids[i]);
            productDao.deleteById(ids[i]);
        }
    }

}
